package com.itheima.utils;

import com.itheima.pojo.OrderSetting;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/15 12:46
 */
public class POIWrite {

    public static void write(List<OrderSetting> orderlist,OutputStream out) {
        XSSFWorkbook workbook = null;
        try {
            //获取文件夹
            workbook = new XSSFWorkbook();
            //获取工作簿
            XSSFSheet sheet = workbook.createSheet();
            //添加表头
            XSSFRow row_title = sheet.createRow(0);
            row_title.createCell(0).setCellValue("日期");
            row_title.createCell(1).setCellValue("可预约人数");
            row_title.createCell(2).setCellValue("已预约人数");


            //循环添加数据
            for (int i = 0; i < orderlist.size(); i++) {
                XSSFRow row = sheet.createRow(i + 1);
                //添加日期
                Date orderDate = orderlist.get(i).getOrderDate();
                System.out.println(orderDate);
                String date = new SimpleDateFormat("yyyy-MM-dd").format(orderDate);
                row.createCell(0).setCellValue(date);
                //添加数量
                int number = orderlist.get(i).getNumber();
                System.out.println(number);
                row.createCell(1).setCellValue(number);
                //添加预定人数
                int reservations = orderlist.get(i).getReservations();
                row.createCell(2).setCellValue(reservations);
            }
            workbook.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
